package com.iphaeton.lms.action.survey;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.common.helper.UserSession;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.organize.IGroupService;
import com.iphaeton.lms.business.organize.IOrganizeService;
import com.iphaeton.lms.business.organize.ITrainService;
import com.iphaeton.lms.business.survey.ISurveyService;
import com.iphaeton.lms.pojo.organize.Organize;
import com.iphaeton.lms.pojo.survey.Survey;
import com.iphaeton.lms.pojo.survey.SurveyQuestion;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

public class SurveyAction extends SurveyBizSupportAction
{
  private static Logger logger;
  private DynaActionForm daf;
  private Pagination pageInfo;
  Calendar cal = Calendar.getInstance();
  Date time = this.cal.getTime();
  DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String currentTime = this.df.format(this.time);
 

  public ActionForward createSurvey(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    this.daf = ((DynaActionForm)form);
    String map = null;
    List list = null;
    String name = (String)this.daf.get("name");
    String date = (String)this.daf.get("date");
    Integer type = (Integer)this.daf.get("type");
    Survey sur = new Survey();
    sur.setDate(date);
    sur.setName(name);
    sur.setCount(new Integer(0));
    sur.setReleaseTime(this.currentTime);
    sur.setType(type);
    try {
      UserSession userSession = (UserSession)request.getSession().getAttribute
        ("userSession");
      String creater = userSession.getId();
      String organizeId = userSession.getManageOrgId();
      sur.setCreater(creater);
      sur.setOrganizeId(organizeId);
      if (type.equals(new Integer(1))) {
        Organize org = this.organize.findOrganizeById(organizeId);
        String str = this.organize.getOrgCkBTree(org, "tree");
        request.setAttribute("str", str);
        map = "organize_assign";
      }
      if (type.equals(new Integer(2))) {
        list = this.group.getGroupList("g", "");
        map = "station_assign";
      }
      if (type.equals(new Integer(3))) {
        list = this.group.getGroupList("z", "");
        map = "duty_assign";
      }
      if (type.equals(new Integer(4))) {
        list = this.train.findAdminTrain(userSession.getUserId());
        map = "train_assign";
      }
      if (type.equals(new Integer(5))){
    	  list = this.group.getGroupList("u", organizeId);
          map = "group_assign";
      }
  
     
    }
    catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }
    label389: request.getSession().setAttribute("survey", sur);
    request.setAttribute("name", name);
    request.setAttribute("list", list);
    return mapping.findForward(map);
  }

  public ActionForward surveyQuestionUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    SurveyQuestion sq;
    String id = request.getParameter("id");
    String question = request.getParameter("question");
    String answer = "";
    List list = null;
    Survey s = null;
    int count = Integer.parseInt(request.getParameter("count"));
    for (int i = 0; i < count; ++i)
      answer = answer + request.getParameter(new StringBuffer(String.valueOf(i)).toString()) + ";;;";

    try
    {
      sq = this.survey.findSurveyQuestionById(id);
      sq.setQuestion(question);
      sq.setAnswer(answer);
      this.survey.addSurveyQuestion(sq);
      list = this.survey.findSurveyQuestion(sq.getSurveyId());
      s = this.survey.findSurveyById(sq.getSurveyId());
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("survey", s);
    request.setAttribute("list", list);
    return mapping.findForward("survey_preview");
  }

  public ActionForward setSurveyGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String id = request.getParameter("groupList");
    String[] group_id = id.split("&");
    Survey sur = (Survey)request.getSession().getAttribute("survey");
    request.getSession().removeAttribute("survey");
    try {
      sur = this.survey.save(sur);
      for (int i = 0; i < group_id.length; ++i)
        this.survey.setSurveyGroup(sur.getId(), group_id[i]);

      sur = this.survey.findSurveyById(sur.getId());
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("survey", sur);
    return mapping.findForward("survey_question");
  }

  public ActionForward setSurveyOrganize(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String orgSelect = request.getParameter("organizeSel");
    String[] organize_id = orgSelect.split("&");
    Survey sur = (Survey)request.getSession().getAttribute("survey");
    request.getSession().removeAttribute("survey");
    try {
      sur = this.survey.save(sur);
      for (int i = 0; i < organize_id.length; ++i)
        this.survey.setSurveyOrganize(sur.getId(), organize_id[i]);

      sur = this.survey.findSurveyById(sur.getId());
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("survey", sur);
    return mapping.findForward("survey_question");
  }

  public ActionForward setSurveyTrain(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String trainList = request.getParameter("trainList");
    String[] train_id = trainList.split("&");
    Survey sur = (Survey)request.getSession().getAttribute("survey");
    request.getSession().removeAttribute("survey");
    try {
      sur = this.survey.save(sur);
      for (int i = 0; i < train_id.length; ++i)
        this.survey.setSurveyTrain(sur.getId(), train_id[i]);

      sur = this.survey.findSurveyById(sur.getId());
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("survey", sur);
    return mapping.findForward("survey_question");
  }

  public ActionForward surveyQuestion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String id = request.getParameter("id");
    Survey sur = null;
    try {
      sur = this.survey.findSurveyById(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("survey", sur);
    return mapping.findForward("survey_question");
  }

  public ActionForward addSurveyQuestion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String map = request.getParameter("map");

    String question = request.getParameter("question");
    String answer = request.getParameter("answer");
    String survey_id = request.getParameter("survey_id");
    Survey sur = null;
    SurveyQuestion surveyQuestion = new SurveyQuestion();
    surveyQuestion.setAnswer(answer);
    surveyQuestion.setQuestion(question);
    surveyQuestion.setSurveyId(survey_id);
    try {
      this.survey.addSurveyQuestion(surveyQuestion);
      sur = this.survey.findSurveyById(survey_id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("survey", sur);
    return mapping.findForward(map);
  }

  public ActionForward previewSurvey(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    List list;
    Survey sur;
    String id = request.getParameter("id");
    try
    {
      list = this.survey.findSurveyQuestion(id);
      sur = this.survey.findSurveyById(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("survey", sur);
    request.setAttribute("list", list);
    return mapping.findForward("survey_preview");
  }

  public ActionForward removeSurveyQuestion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    List list;
    Survey sur;
    String question_id = request.getParameter("question_id");
    String id = request.getParameter("id");
    try
    {
      this.survey.removeSurveyQuestion(question_id);
      list = this.survey.findSurveyQuestion(id);
      sur = this.survey.findSurveyById(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("survey", sur);
    request.setAttribute("list", list);
    return mapping.findForward("survey_preview");
  }

  public ActionForward removeSurvey(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String[] id = request.getParameter("id").split(";");
    try {
      for (int i = 0; i < id.length; ++i)
        this.survey.remove(id[i]);
    }
    catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("id", id);
    return mapping.findForward("surveyManage");
  }

  public ActionForward editSurveyQuestion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    SurveyQuestion sq;
    String id = request.getParameter("id");
    try
    {
      sq = this.survey.findSurveyQuestionById(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("question", sq);
    return mapping.findForward("survey_question_edit");
  }

  public ActionForward surveyManage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    List list = null;
    try {
      String user_id = ((UserSession)request.getSession().getAttribute(
        "userSession")).getId
        ();
      list = this.survey.surveyManage(user_id, this.currentTime);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }
    request.setAttribute("list", list);
    return mapping.findForward("survey_manage");
  }

  public ActionForward surveyLast(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    List list = null;
    try {
      String user_id = ((UserSession)request.getSession().getAttribute(
        "userSession")).getId
        ();
      list = this.survey.surveyLast(user_id, this.currentTime);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }
    request.setAttribute("list", list);
    return mapping.findForward("survey_last");
  }

  public ActionForward currentSurvey(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    List list = null;
    try {
      UserSession userSession = (UserSession)request.getSession().getAttribute
        ("userSession");
      String user_id = userSession.getId();
      String organize_id = userSession.getOrgId();
      list = this.survey.findCurrentSurvey(user_id, organize_id, this.currentTime);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }
    request.setAttribute("list", list);
    return mapping.findForward("current_survey");
  }

  public ActionForward completeSurvey(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    return mapping.findForward("complete_survey");
  }

  public ActionForward surveyResult(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    Survey sur;
    String id = request.getParameter("id");
    List list = null;
    try
    {
      sur = this.survey.findSurveyById(id);
      list = this.survey.surveyResult(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("list", list);
    request.setAttribute("survey", sur);
    return mapping.findForward("survey_result");
  }

  public ActionForward openSurvey(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String id = request.getParameter("id");
    Survey sur = null;
    List list = null;
    try {
      sur = this.survey.findSurveyById(id);
      list = this.survey.findSurveyQuestion(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    }
    request.setAttribute("survey", sur);
    request.setAttribute("list", list);
    return mapping.findForward("open_survey");
  }

  public ActionForward answerSurvey(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    String id = request.getParameter("id");

    List list = null;
    try {
      String user_id = ((UserSession)request.getSession().getAttribute(
        "userSession")).getId
        ();
      list = this.survey.findSurveyQuestion(id);
      for (int i = 0; i < list.size(); ++i) {
        String user_answer = request.getParameter(String.valueOf(i));
        if (user_answer == null)
          return mapping.findForward("failure");

        String qid = ((SurveyQuestion)list.get(i)).getId();
        this.survey.answerSurvey(user_id, qid, user_answer);
      }
      this.survey.countSurvey(id);
    } catch (BusinessException e) {
      e.printStackTrace();
      request.setAttribute("exception", e);
      return mapping.findForward("failure");
    } catch (NullPointerException ne) {
      return mapping.findForward("session_dead");
    }
    request.setAttribute("surveyId", id);
    return mapping.findForward("survey_answer_ok");
  }
}